package com.quan.commons.log.aspect;

import com.quan.admin.entity.SysLog;
import com.quan.commons.core.utils.SpringContextHolder;
import com.quan.commons.log.event.SysLogEvent;
import com.quan.commons.log.utils.SysLogUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


/**
 * 操作日志使用spring event异步入库
 */
@Slf4j
@Aspect
@Component
public class SysLogAspect {

	@Around("@annotation(sysLog)")
	@SneakyThrows
	public Object around(ProceedingJoinPoint point, com.quan.commons.log.annotation.SysLog sysLog) {
		String strClassName = point.getTarget().getClass().getName();
		String strMethodName = point.getSignature().getName();
		log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName);

		Long startTime = System.currentTimeMillis();
		Object obj = point.proceed();
		Long endTime = System.currentTimeMillis();

		final SysLog systemLog = SysLogUtils.getSysLog();
		// 日志类型
		systemLog.setType(sysLog.type().getIndex());
		// 日志标题
		systemLog.setTitle(sysLog.value());
		// 执行时间
		systemLog.setTime(endTime - startTime);

		// 发送异步日志事件
		SpringContextHolder.publishEvent(new SysLogEvent(systemLog));

		return obj;
	}

}
